package com.ProdigiousAT;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.ProdigiousAT.DDF;

public class SecondEvaluation {

    int iWait;
    private static WebDriver wd;

    @Before
    public void setUp(){
        wd = new FirefoxDriver();
        //wd = new ChromeDriver();
        iWait = 2000;
    }

    @Test
    public void myMainTest() throws Exception {
        //DDF instance
        DDF dataDrivenFramework = new DDF();

        //Local Variables  to store the TestData
        String siteURL, SearchTerm, Filter1, Filter2, ExpectedResults, ActualResults;


        //Read each set of test data from the excel
        //Declare de XL file path
        String xlPath = "C:/Users/Worker/Desktop/SecondEvaluation.xls";
        // "Sheet1" is the tab name in the excel file
        String[][] myXL = dataDrivenFramework.readXL(xlPath, "Sheet1");

        //Take values form eah row in 2D local array "myXL" and load them into local variables
        for(int k=1; k<myXL.length; k++){
            siteURL = myXL[k][0];
            SearchTerm = myXL[k][1];
            Filter1 = myXL[k][2];
            Filter2 = myXL[k][3];
            ExpectedResults = myXL[k][4];

            //Execute test
            wd.get(siteURL);
            wd.findElement(By.xpath("//input[@id='masthead-search-term']")).sendKeys(SearchTerm);

            wd.findElement(By.xpath("//button[@id='search-btn']")).click();
            Thread.sleep(iWait);


            if(!Filter1.equals("*")){
                wd.findElement(By.cssSelector(".yt-uix-button.yt-uix-button-size-small.yt-uix-button-default.filter-button.yt-uix-expander-head")).click();
                wd.findElement(By.xpath("//*[@id='filter-dropdown']/div/ul/li/a/span[text()='"+Filter1+"']")).click();
            }
            if(!Filter2.equals("*")){
                wd.findElement(By.cssSelector(".yt-uix-button.yt-uix-button-size-small.yt-uix-button-default.filter-button.yt-uix-expander-head")).click();
                wd.findElement(By.xpath("//*[@id='filter-dropdown']/div/ul/li/a/span[text()='"+Filter2+"']")).click();
            }
            Thread.sleep(iWait);

            ActualResults = wd.findElement(By.cssSelector(".num-results.first-focus")).getText();
            System.out.println("Your search result is: " + ActualResults+"\n");

            //Write column "Obtained Output"
            myXL[k][6] = ActualResults;
            //Validate if the test passed or failed
            if(ActualResults.equals(ExpectedResults)) {
               //Write column Results
               myXL[k][5] = "Passed!";
               System.out.print("Test Passed!\n");
            }else {
               //Write column Results
               myXL[k][5] = "Failed";
               System.out.print("Test Failed!\n");
            }
        }
        dataDrivenFramework.writeXL("C:/Users/Worker/Desktop/ResultsSecondEvaluation.xls", "Results", myXL);
    }

    @AfterClass
    public static void tearDown(){
        wd.close();
        wd.quit();
    }
}
